/**
 * Copyright (c) 2008 Luciano Bustos - riaevolution.com, All Rights Reserved.
 *
 * Contact luchyx@riaevolution.com
 * See www.riaevolution.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * */
package com.riaevolution.components.iconhelp
{
	import mx.rpc.AsyncToken;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;

	/**
	 * Helper class to load content from wikipedia. 
	 */
	public class WikipediLoadStrategy extends HelpStrategy
	{
		/**
		 * Constructor 
		 */
		public function WikipediLoadStrategy()
		{
			super();
			loader = new HTTPService();
			loader.resultFormat = "text";
		}
		
		private var loader:HTTPService;
		/**
		 * Handler where we parse the wiki result 
		 * @param event
		 * 
		 */		
		protected function handleResult(event:ResultEvent):void {
			var string:String = event.result.toString();
			var strat:HelpStrategy = event.token.strategy as HelpStrategy;
			if(strat) {
				
				var title:Array = RegExp(/\<title\>(.+)<\/title\>/gi).exec(string);
				//seteo el titulo
				if(title.length > 1) {
					strat.setTitle(title[1]);	
				}
				
				string = string.split("<!-- bodytext -->")[1];
				string = string.split("<!-- /bodytext -->")[0];
				string = string.replace(/(\r+|\n+)/ig,"");
				string = string.substr(0,850) + "...";
				strat.setContent(string);
			}
		}
		/**
		 * handler for the fault 
		 * @param event
		 */		
		protected function handleFault(event:FaultEvent):void {
			var strat:HelpStrategy = event.token.strategy as HelpStrategy;
			if(strat) {
				strat.setContent(event.fault.faultString);
			}
		}
		
		/**
		 * @inheritDoc 
		 */
		override public function load(icon:HelpIcon):void {
			super.load(icon);
			if(loader) {
				loader.url = icon.link;
				var token:AsyncToken = loader.send();
				token.addResponder(new Responder(handleResult, handleFault));
				setContent("Please wait..");
				token.strategy = this;
			}
		}
		
	}
}